package collectionTaks;

import collection.LambdaDemo;

import java.util.*;

/** 对List集合排序：排Integer、排String、排Point----练习Lambda表达式使用 */
public class ListSortDemo {
    public static void main(String[] args) {
        //排序Integer
        List<Integer> list = new ArrayList<>();
        Random rand = new Random();
        for (int i = 0; i < 10; i++) {
            list.add(rand.nextInt(100));
        }
        System.out.println("list:"+list);//list[17, 67, 66, 20, 67, 95, 46, 4, 2, 5]

        Collections.sort(list);//升序(从小到大)
        System.out.println("排序后list:"+list);//排序后list:[5, 13, 15, 18, 27, 34, 43, 44, 72, 79]

        //排序String
        List<String> list1 = new ArrayList<>();
        list1.add("王大锤");
        list1.add("狗蛋");
        list1.add("爱因斯坦");
        //按ASCII码排序
        //从第一个字开始排列
        Collections.sort(list1);
        System.out.println(list1);//[爱因斯坦, 狗蛋, 王大锤]
        //自定义排序
        Collections.sort(list1, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();//升序
            }
        });
        System.out.println("自定义排序list1:"+list1);//[狗蛋, 王大锤, 爱因斯坦]

        //Lambda表达式
        Collections.sort(list1, (o1,o2)-> o1.length()-o2.length());
        System.out.println(list1);//[狗蛋, 王大锤, 爱因斯坦]


        //排Point
        List<Point> list2 = new ArrayList<>();
        list2.add(new Point(1,2));
        list2.add(new Point(2,9));
        list2.add(new Point(22,45));
        list2.add(new Point(18,8));
        list2.add(new Point(34,0));
        System.out.println(list2);//[(1,2), (2,9), (22,45), (18,8), (34,0)]

        Collections.sort(list2, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                return o1.getX()- o2.getX();//按x坐标排序(升序)
            }
        });
        System.out.println(list2);//[(1,2), (2,9), (18,8), (22,45), (34,0)]

        //Lambda表达式
        Collections.sort(list2, (o1,o2)->o1.getX()- o2.getX());
        System.out.println(list2);//[(1,2), (2,9), (18,8), (22,45), (34,0)]






















    }
}
